package com.alice.springcloud.controller;

import com.alice.springcloud.pojo.Dept;
import com.alice.springcloud.service.DeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//提供restful服务
@RestController
@RequiredArgsConstructor
public class DeptController {

    private final DeptService deptService;

    private final DiscoveryClient client;

    //注册进来的微服务，获取一些信息。没有实际作用
    @RequestMapping("/dept/discovery")
    public Object discovery() {
        //获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services " + services);

        //得到一个具体的微服务信息,通过具体的微服务id，7001中的applicationName
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost() + "\t\t\t" +
                            instance.getPort() + "\t\t\t" +
                            instance.getUri() + "\t\t\t" +
                            instance.getServiceId());
        }
        return this.client;
    }

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept){
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return deptService.queryById(id);
    }

    @GetMapping("/dept/list")
    public List<Dept> getAll(){
        return deptService.queryAll();
    }
}
